package com.neuosft.his.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.Soundbank;

import com.neuosft.his.entity.CommonResult;
import com.neuosft.his.entity.Department;
import com.neuosft.his.service.IDepartmentService;
import com.neuosft.his.service.impl.DepartmentServiceImpl;

@WebServlet("/dept.do")
public class DepartmentController extends HttpServlet {
	IDepartmentService departmentService = new DepartmentServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 提取表单数据
		String string = request.getParameter("id");
		int id = string == null ? -1 : Integer.parseInt(string);
		String dept_code = request.getParameter("dept_code");
		String dept_name = request.getParameter("dept_name");
		String dept_type = request.getParameter("dept_type");
		string = request.getParameter("delmark");
		int delmark = string == null ? -1 : Integer.parseInt(string);
		Department department = new Department(id, dept_code, dept_name, dept_type, delmark);
		// 判断执行业务
		String action = request.getParameter("action");
		if (action == null || action.trim().length() == 0) {
			action = "xxx";
		}
		// 执行业务
		switch (action) {
		case "add":
			doAdd(request, response, department);
			break;
		case "mod":
			domod(request, response, department);
			break;
		case "all":
			doAll(request, response, department);
			break;
		case "byid":
			doByid(request, response, department);
			break;
		default:
			doDefault(request, response);
			break;
		}
	}

	private void doDefault(HttpServletRequest request, HttpServletResponse response) {
		CommonResult result = new CommonResult();
		result.badRequest();
		request.setAttribute(CommonResult.RESULT, result);
	}

	private void doByid(HttpServletRequest request, HttpServletResponse response, Department department) {
		// TODO Auto-generated method stub

	}

	private void doAll(HttpServletRequest request, HttpServletResponse response, Department department) {
		CommonResult result = departmentService.findAll();
		request.setAttribute(CommonResult.RESULT, result);
	}

	private void domod(HttpServletRequest request, HttpServletResponse response, Department department) {
		
		CommonResult result = departmentService.modDepartment(department);
		request.setAttribute(CommonResult.RESULT, result);
	}

	private void doAdd(HttpServletRequest request, HttpServletResponse response, Department department) {
		System.out.println(department);
		IDepartmentService departmentService = new DepartmentServiceImpl();
		CommonResult result = departmentService.addDepartment(department);
		request.setAttribute(CommonResult.RESULT, result);

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}

}
